package com.yvdedu.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yvdedu.entity.Course;
import com.yvdedu.entity.User;
import com.yvdedu.service.Course_Mgr_Service;
import com.yvdedu.service.Teacher_Mgr_Service;

@Controller
public class Mgr_Tea_Controller {

	private Teacher_Mgr_Service tms = new Teacher_Mgr_Service();
	private List<User> userList = new ArrayList<User>();
	private List<Course> courses = new ArrayList<Course>();
	private User user = new User();
	private Course_Mgr_Service cms = new Course_Mgr_Service();

	@RequestMapping("append.tch")
	public String append(HttpServletRequest request, Model model)
			throws Exception {

		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("tchName");
		String phone = request.getParameter("phone");
		String mailbox = request.getParameter("mailbox");
		String passWord = request.getParameter("passWord");

		tms.append(name, phone, mailbox, passWord);
		model.addAttribute("cre_teacher", true);
		return "create_Teacher";

	}

	@RequestMapping("delete_teacher")
	public String delTeacher(HttpServletRequest request) {

		int id = Integer.valueOf(request.getParameter("id"));
		tms.delete(id);
		return "query_Teacher";
	}

	@RequestMapping("editTeacher.do")
	public String editTeacher(HttpServletRequest request, HttpSession session)
			throws Exception {
		request.setCharacterEncoding("UTF-8");
		int id = (int) session.getAttribute("mar.tea.id");
		String name = request.getParameter("tchName");
		String nicheng = request.getParameter("nicheng");
		String sex = request.getParameter("sex");
		int age = Integer.valueOf(request.getParameter("age"));
		String birthday = request.getParameter("birthday");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String qq = request.getParameter("qq");
		String dizhi = request.getParameter("dizhi");
		String createDate = request.getParameter("createDate");
		String pwd = request.getParameter("pwd");

		tms.update(id, name, nicheng, sex, age, birthday, phone, email, qq,
				dizhi, createDate, pwd);
		return "query_Teacher";
	}

	@RequestMapping("mgr.tea.edit")
	public String editTeacher(HttpServletRequest request, Model model,
			HttpSession session) {

		int id = Integer.valueOf(request.getParameter("id"));
		user = tms.query(id);
		System.out.println(user.getName());
		model.addAttribute("teacher", user);
		session.setAttribute("mar.tea.id", id);
		return "edit_teacher";
	}

	@RequestMapping("mgr.tea.create")
	public String hander() {
		return "create_Teacher";
	}

	@RequestMapping("query.tch")
	public String queryDo(HttpServletRequest req, Model model) throws Exception {
		req.setCharacterEncoding("UTF-8");
		String str1 = req.getParameter("field");
		String str2 = req.getParameter("fieldType");

		System.out.println(str1);
		System.out.println(str2);
		if ("".equals(str1)) {
			userList = tms.query();
			model.addAttribute("userList", userList);
		} else if (!"".equals(str1)) {
			userList = tms.query(str1, str2);
			model.addAttribute("userList", userList);
		}
		return "query_Teacher";

	}

	@RequestMapping("mgr.tea.query")
	public String queryGo() {
		return "query_Teacher";
	}

	@RequestMapping("tea.append.course")
	public String tea_append_course(HttpServletRequest req, Model model,
			HttpSession session) throws Exception {

		req.setCharacterEncoding("UTF-8");
		int couId = Integer.parseInt(req.getParameter("couId"));
		int teaId = Integer.parseInt(req.getParameter("teaId"));
		System.out.println(couId);
		System.out.println(teaId);

		cms.append(couId, teaId);
		model.addAttribute("courseList", courses);

		return "append_course_teacher";
	}

	@RequestMapping("tea_course")
	public String tea_course(HttpServletRequest request, HttpSession session) throws Exception {
		
		request.setCharacterEncoding("UTF-8");
		int id = Integer.parseInt(request.getParameter("userId"));
		System.out.println(id);
		user = tms.query(id);
		courses = cms.queryCou(id);
		session.setAttribute("teaId", id);
		session.setAttribute("teacher", user);
		session.setAttribute("teaCourse", user);
		return "append_course_teacher";

	}

	@RequestMapping("tea.search.course")
	public String tea_search_course(HttpServletRequest req, Model model)
			throws Exception {
		req.setCharacterEncoding("UTF-8");
		String field = req.getParameter("field");
		String type = req.getParameter("fieldType");

		if (!"".equals(field)) {
			courses = cms.query(field, type);
			model.addAttribute("courseList", courses);
		}
		return "append_course_teacher";
	}

	@RequestMapping("yzm.validate")
	@ResponseBody
	public String validate(HttpServletRequest request, HttpSession session) {
		String yzm = request.getParameter("yzm");
		String code = (String) session.getAttribute("rand");
		if (yzm.equalsIgnoreCase(code)) {
			return "true";
		} else {
			return "false";
		}
	}

}
